// pages/home/home.js
const app = getApp()
const db = wx.cloud.database()
var userlatitude = ''
var userlongitude = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    statusBarHeight: app.globalData.statusBarHeight,
    list: '',
    list1:'',
    shelterid:'',
    Height: 0,
    scale: 13,
    latitude: "",
    longitude: "",
    markers: [],
    controls: [{
        id: 1,
        iconPath: 'cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/location.png',
        position: {
          left: 320,
          top: 100 - 50,
          width: 20,
          height: 20
        },
        clickable: true
      },
      {
        id: 2,
        iconPath: 'cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/location.png',
        position: {
          left: 340,
          top: 100 - 50,
          width: 20,
          height: 20
        },
        clickable: true
      }
    ],
    circles: []
  },

  navigateBack:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;

    wx.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        _this.setData({
          view: {
            Height: res.windowHeight
          }

        })

      }
    })

    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        getApp().globalData.userlatitude = res.latitude
        console.log(app.globalData.userlatitude)
        getApp().globalData.userlongitude = res.longitude
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            id: 1,
            latitude: res.latitude,
            longitude: res.longitude,
            width: 50,
            height: 50,
            iconPath: "cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/location.png",
            title: "哪里"

          }],
          circles: [{
            latitude: res.latitude,
            longitude: res.longitude,
            color: '#FF0000DD',
            fillColor: '#7cb5ec88',
            radius: 1000,
            strokeWidth: 1
          }]

        })

        _this.getDistance()
        var log1=_this.data.shelterid
        console.log(log1)


      }


    })


  },

  regionchange(e) {
    console.log("regionchange===" + e.type)
  },

  //点击markers
  markertap(e) {
    console.log(e.markerId)

    wx.showActionSheet({
      itemList: ["A"],
      success: function (res) {
        console.log(res.tapIndex)
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  //点击缩放按钮动态请求数据
  controltap(e) {
    var that = this;
    console.log("scale===" + this.data.scale)
    if (e.controlId === 1) {
      // if (this.data.scale === 13) {
      that.setData({
        scale: --this.data.scale
      })
      // }
    } else {
      //  if (this.data.scale !== 13) {
      that.setData({
        scale: ++this.data.scale
      })
      // }
    }

  },
  shelter1: function (latFrom, lngFrom, latTo, lngTo) {
    const cont = db.collection('eas-shelter')
    cont.get({
      success: res => {
        this.setData({
          list: res.data,
        })
        var latitude = this.data.list[0].locate.latitude
        var longitude = this.data.list[0].locate.longitude
        latTo = getApp().globalData.userlatitude
        lngTo = getApp().globalData.userlongitude
        console.log(latTo + '的' + lngTo)
        console.log(latitude + '的' + longitude)
        for (var i = 0; i <= this.data.list.length; i++) {
          latFrom = this.data.list[i].locate.latitude
          lngFrom = this.data.list[i].locate.longitude
          console.log(latTo)
          var rad = function (d) { //计算角度
            return d * Math.PI / 180.0;
          }
          var EARTH_RADIUS = 6378136.49;
          var radLatFrom = rad(latFrom);
          var radLatTo = rad(latTo);
          var a = radLatFrom - radLatTo;
          var b = rad(lngFrom) - rad(lngTo);
          var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
          distance = distance * EARTH_RADIUS;
          distance = Math.round(distance * 10000) / 10000;
          console.log(parseFloat(distance.toFixed(0)))
          this.setData({
            distance1: parseFloat(distance.toFixed(0))
          })
          if (this.data.distance1 <= 10000) {
            var shelterid=this.data.list[i]._id
            console.log(shelterid)
            const cont=db.collection('eas-shelter')
            var _this = this;
            cont.where({
              _id:shelterid
            }).get({
              success:res=>{
                this.setData({
                  list1:res.data[0]
                })
                console.log(res)
                _this.setData({
                  latitude: this.data.list1.locate.latitude,
                  longitude: this.data.list1.locate.longitude,
                  markers: [{
                    id: 1,
                    latitude: this.data.list1.locate.latitude,
                    longitude: this.data.list1.locate.longitude,
                    width: 50,
                    height: 50,
                    iconPath: "cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/location.png",
                    title: "哪里"
            
                  }],
                  circles: [{
                    latitude: this.data.list1.locate.latitude,
                    longitude: this.data.list1.locate.longitude,
                    color: '#FF0000DD',
                    fillColor: '#7cb5ec88',
                    radius: 1000,
                    strokeWidth: 1
                  }]
            
                })
            
              }
            })
          }
        }

      }
    })

 

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },
  getDistance(latFrom, lngFrom, latTo, lngTo) {
    const cont = db.collection('eas-shelter')
    cont.get({
      success: res => {
        this.setData({
          list: res.data,
        })
        var latitude = this.data.list[0].locate.latitude
        var longitude = this.data.list[0].locate.longitude
        latTo = getApp().globalData.userlatitude
        lngTo = getApp().globalData.userlongitude
        console.log(latTo + '的' + lngTo)
        console.log(latitude + '的' + longitude)
        for (var i = 0; i <= this.data.list.length; i++) {
          latFrom = this.data.list[i].locate.latitude
          lngFrom = this.data.list[i].locate.longitude
          console.log(latTo)
          var rad = function (d) { //计算角度
            return d * Math.PI / 180.0;
          }
          var EARTH_RADIUS = 6378136.49;
          var radLatFrom = rad(latFrom);
          var radLatTo = rad(latTo);
          var a = radLatFrom - radLatTo;
          var b = rad(lngFrom) - rad(lngTo);
          var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
          distance = distance * EARTH_RADIUS;
          distance = Math.round(distance * 10000) / 10000;
          console.log(parseFloat(distance.toFixed(0)))
          this.setData({
            distance1: parseFloat(distance.toFixed(0))
          })
          if (this.data.distance1 <= 10000) {
            var shelterid=this.data.list[i]._id
            console.log(shelterid)
            const cont=db.collection('eas-shelter')
            cont.where({
              _id:shelterid
            }).get({
              success:res=>{
                this.setData({
                  list1:res.data[0]
                })
                console.log(res)
              }
            })
          }
        }

      }
    })


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



})